package com.canbot.u05.activity.shoppingGuide.merchandiseShoppingGuide;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.bean.sales.ChoosePicBean;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.ToastTools;

import java.util.HashMap;
import java.util.List;

/**
 *
 */

public class MerchandisePicAdapter extends RecyclerView.Adapter<MerchandisePicAdapter.MerchandisePhotoItem> {

        private Context context;

        private List<ChoosePicBean> data;

        private LayoutInflater inflater;

        private MerchandisePhotoItem holder;

        private OnPhotoItemCheckedListener onPhotoItemCheckedListener;

        private OnItemClickListener onItemClickListener;


        private List<ChoosePicBean> selectedList;

        /**
         * 防止Checkbox错乱 做setTag  getTag操作
         */
        public HashMap<Integer, Boolean> mCheckStates = new HashMap<>();


        public MerchandisePicAdapter(Context context, List<ChoosePicBean> data, OnPhotoItemCheckedListener onPhotoItemCheckedListener,
                                     OnItemClickListener onItemClickListener) {
                this.context = context;
                this.data = data;
                this.onPhotoItemCheckedListener = onPhotoItemCheckedListener;
                this.onItemClickListener = onItemClickListener;
                inflater = LayoutInflater.from(context);
                init();
        }

        // 初始化 设置所有item都为未选择
        public void init() {

                for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).isChecked()) {
                                mCheckStates.put(i, data.get(i).isChecked());
                        }
                }
        }


        @NonNull
        @Override
        public MerchandisePhotoItem onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View m = inflater.inflate(R.layout.item_select_merchandise_pic, parent, false);
                return new MerchandisePhotoItem(m);
        }

        @Override
        public void onBindViewHolder(@NonNull MerchandisePhotoItem holder, final int position) {
                this.holder = holder;
                holder.checkBox.setTag(position);
                holder.position = position;

                final ChoosePicBean photo = data.get(position);
                holder.loadView(photo, position);

                holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(final CompoundButton compoundButton, boolean isChecked) {
                                int index = (int) compoundButton.getTag();

                                if (isChecked) {
                                        mCheckStates.put(position, true);
                                }
                                else {
                                        mCheckStates.remove(position);
                                }
                                photo.setChecked(isChecked);
                                onPhotoItemCheckedListener.onCheckedChanged(position, photo, compoundButton, isChecked);
                        }
                });

                if (mCheckStates.size() > 5) {
                        ToastTools.newInstance(context).showText(context.getString(R.string.Choose_five_most));
                        holder.checkBox.setChecked(false);
                        photo.setChecked(false);
                        mCheckStates.remove(position);
                }

                if (mCheckStates != null && mCheckStates.containsKey(position)) {
                        holder.checkBox.setChecked(true);
                        photo.setChecked(true);
                }
                else {
                        holder.checkBox.setChecked(false);
                        photo.setChecked(false);
                }

        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public int getItemCount() {
                return data.size();
        }

        class MerchandisePhotoItem extends RecyclerView.ViewHolder implements View.OnClickListener {

                public int position;

                private ImageView ivPhoto;

                private TextView textView;

                private CheckBox checkBox;

                private ChoosePicBean photo;

                public MerchandisePhotoItem(View itemView) {
                        super(itemView);

                        textView = (TextView) itemView.findViewById(R.id.tv);
                        ivPhoto = (ImageView) itemView.findViewById(R.id.iv_map);
                        checkBox = (CheckBox) itemView.findViewById(R.id.cb_select);
                        ivPhoto.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                        onItemClickListener.onItemClick(position);
                }


                public void loadView(ChoosePicBean photo, int position) {
                        this.position = position;
                        this.photo = photo;

                        //                        Logger.e(TAG,"loadView 1" +data.get(position).getOriginalPath());
                        int i = position + 1;
                        String path = data.get(position).getImagePath();
                        if (photo.getImagePath().contains("/uupic/")) {
                                textView.setText(photo.getImagePath().replace("/uupic/", ""));
                        }
                        String url = IpMsgConst.HTTP_URL_CHEST + photo.getImagePath().replace("/uupic/", "/uupic_thumb/");
                        //                                       MainApplication.instance.displayImage(url+photo.getOriginalPath(),ivPic);
                        GlideApp.with(context).clear(ivPhoto);
                        GlideApp.with(context)
                                .asDrawable()
                                .load(url)
                                .thumbnail(0.1f)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                .skipMemoryCache(false)
                                .placeholder(R.drawable.sales_banner01)//占位图
                                .error(R.drawable.ic_picture_loadfailed)
                                .override(100, 100)
                                .into(ivPhoto);

                }

                public CheckBox getCheckBox() {
                        return holder.checkBox;
                }
        }

        public void updateItem(int position, ChoosePicBean choosePicBean, List<ChoosePicBean> selectedList) {
                this.selectedList = selectedList;
                if (!choosePicBean.isChecked()) {
                        mCheckStates.remove(position); // 修改map的值保存状态
                }

                //                        int index = this.data.indexOf(choosePicBean);
                //                        if(index >= 0) {
                //                                this.data.set(index, choosePicBean);
                //                                this.notifyItemChanged(index);
                this.notifyItemChanged(position);
                //                        }

        }

        /**
         * 图片Item选中事件监听器
         */
        public interface OnPhotoItemCheckedListener {

                void onCheckedChanged(int posiion, ChoosePicBean photoModel, CompoundButton buttonView, boolean isChecked);
        }

        /**
         * 图片点击事件
         */
        public interface OnItemClickListener {

                void onItemClick(int position);
        }
}
